<template>
  <div class="classifybox">
    <div class="classify">
      <el-button type="primary">+新增</el-button>

      <el-table :data="Data" border style="width: 100%;margin: 30px 0;">
        <el-table-column prop="categoryName" label="分类名称" align="center" width="180" />
        <el-table-column label="图片" width="180" align="center">
          <template #default="scope">
            <div style="display: flex; align-items: center;justify-content: space-around;">
              <img :src="'https://img.mall4j.com/' + scope.row.pic" alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <span>{{ scope.row.status == 1 ? "正常" : "不正常" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="序号" align="center">
          <template #default="scope">
            <span>{{ scope.row.seq }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <el-button type="primary" @click="dialogFormVisible = true" plain>修改</el-button>
          <el-button type="danger" @click="open" plain>删除</el-button>
        </el-table-column>
      </el-table>
    </div>


    <!-- 弹框 -->
    <el-dialog v-model="dialogFormVisible" title="修改">
      <el-form :model="form">
        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>

        <el-form-item label="上级分类" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>

        <el-form-item label="排序号" :label-width="formLabelWidth">
          <el-input-number v-model="num" :min="1" :max="10" controls-position="right" size="large"
            @change="handleChange" />
        </el-form-item>

        <el-form-item label="" :label-width="formLabelWidth">
          <div class="mb-2 flex items-center text-sm">
            <el-radio-group v-model="radio1" class="ml-4">
              <el-radio label="1" size="large">下线</el-radio>
              <el-radio label="2" size="large">正常</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts"  setup>
import { classify } from '../../api/index'
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, UploadProps, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const imageUrl = ref('')
const Data = ref([])
const dialogFormVisible = ref(false)
//获取数据
onMounted(async () => {
  const res = await classify()
  console.log(res)
  Data.value = res.data
})

//弹出框
const formLabelWidth = '140px'
const form = reactive({
  name: '',
  region: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
})

//上传
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

//数字输入框
const num = ref(1)
const handleChange = (value: number) => {
  console.log(value)
}
//单选
const radio1 = ref('1')

//删除确认
const open = () => {
  ElMessageBox.confirm(
    '确认删除?',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      ElMessage({
        type: 'success',
        message: 'Delete completed',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled',
      })
    })
}
</script>

<style>
.classifybox {
  width: 100%;
  height: 100%;
}

.classify {
  width: 98%;
  background: white;
  margin: 10px;
  padding: 20px;
}

img {
  width: 161px;
  height: 80px;
}

.el-button--text {
  margin-right: 15px;
}

.el-select {
  width: 300px;
}

.el-input {
  width: 300px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}




/* 上传 */
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}


/* 确认弹出框 */
.el-overlay {
  display: flex;
  justify-content: space-evenly;
  flex-wrap: wrap;
  align-content: center;
  color: white;
}

.el-message-box__header {
  display: flex;
  justify-content: space-between;
  background: white;
}

.el-message-box__container {
  display: flex;
  justify-content: flex-start;
  align-content: stretch;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  background: white;
  color: black;
}

.el-message-box__btns {
  background: white;
}
</style>